// Fill out your copyright notice in the Description page of Project Settings.


#include "Player/AuraPlayerController.h"
#include "EnhancedInputSubsystems.h"
#include "EnhancedInputComponent.h"
#include "Interaction/EnemyInterface.h"

AAuraPlayerController::AAuraPlayerController()
{
	bReplicates = true;															//游戏同步，可复制
}

void AAuraPlayerController::PlayerTick(float DeltaTime)
{
	Super::PlayerTick(DeltaTime);
	CursorTrace();
	
}

void AAuraPlayerController::CursorTrace()
{
	FHitResult CursorHit;														//光标命中结果
	GetHitResultUnderCursor(ECC_Visibility, false, CursorHit);		//获取光标下的命中结果
	if (!CursorHit.bBlockingHit) return;										//鼠标无阻挡命中

	LastActor = ThisActor;
	ThisActor = Cast<IEnemyInterface>(CursorHit.GetActor());				//检查是否实现了接口"IEnemyInterface"

	/*
	 * 从光标进行线追踪有几种场景:
	 * 1.LastActor为null && ThisActor为null
	 *		什么都不做
	 * 2.LastActor为null && ThisActor为valid
	 *		ThisActor高亮
	 * 3.LastActor为valid && ThisActor为null
	 *		LastActor取消高亮
	 * 4.LastActor为valid && ThisActor为valid
	 *		{
	 *			4-1.LastActor == ThisActor
	 *				什么都不做
	 *			4-2.LastActor != ThisActor
	 *				LastActor取消高亮
	 *				ThisActor高亮
	 *		}
	 */
	
		if (LastActor == nullptr)
		{
			//1.LastActor为null && ThisActor为null		什么都不做
			//2.LastActor为null && ThisActor为valid		高亮
			if (ThisActor != nullptr)
			{
				ThisActor->HighLightAction();
			}
		} else
		{	// 3.LastActor为valid && ThisActor为null		取消高亮
			if (ThisActor == nullptr)
			{
				LastActor->UnHighLightAction();
			}else
			{
				//4-1.LastActor == ThisActor		什么都不做
				//4-2.LastActor != ThisActor		LastActor取消高亮		ThisActor高亮
				if (LastActor != ThisActor)
				{
					LastActor->UnHighLightAction();
					ThisActor->HighLightAction();
				}
			}
		}
		
	
	
}


void AAuraPlayerController::BeginPlay()
{
	Super::BeginPlay();
	check(AuraContext);															//检查AuraContext是否存在
	
	/*输入上下文AuraContext绑定到玩家控制器*/
	TObjectPtr<UEnhancedInputLocalPlayerSubsystem> Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(GetLocalPlayer());
																				//增强输入本地玩家子系统
	check(Subsystem);															//检查Subsystem是否存在
	Subsystem->AddMappingContext(AuraContext,0);						//添加映射上下文

	/*鼠标设置*/
	bShowMouseCursor = true;													//显示鼠标光标
	DefaultMouseCursor = EMouseCursor::Default;									//默认光标

	/*输入模式游戏和UI设置*/
	FInputModeGameAndUI InputModeData;
	InputModeData.SetLockMouseToViewportBehavior(EMouseLockMode::DoNotLock);	//光标锁定
	InputModeData.SetHideCursorDuringCapture(false);							//隐藏光标
	SetInputMode(InputModeData);												

}

void AAuraPlayerController::SetupInputComponent()
{
	Super::SetupInputComponent();
	TObjectPtr<UEnhancedInputComponent> EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(InputComponent);
																				//类型转换检查
	/*将移动输入动作绑定到增强输入组件*/
	EnhancedInputComponent->BindAction(MoveAction,ETriggerEvent::Triggered,this,&AAuraPlayerController::Move);
}

void AAuraPlayerController::Move(const struct FInputActionValue& InputActionValue)
{
	const FVector2d InputAxisVector = InputActionValue.Get<FVector2d>();		//获取轴向量
	const FRotator Rotation = GetControlRotation();								//获取控制旋转
	const FRotator YawRotator(0.f,Rotation.Yaw,0.f);				//偏航旋转

	const FVector ForwardDirection = FRotationMatrix(YawRotator).GetUnitAxis(EAxis::X);
	const FVector RightDirection = FRotationMatrix(YawRotator).GetUnitAxis(EAxis::Y);
	
	if (TObjectPtr<APawn> ControledPawn = GetPawn<APawn>())
	{
		ControledPawn->AddMovementInput(ForwardDirection,InputAxisVector.Y);
		ControledPawn->AddMovementInput(RightDirection,InputAxisVector.X);
	}


	
}

